package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    /**
     * 缓存名
     */
    public static final String CACHE_NAMES = "test_cache#600";

    /**
     * 缓存key
     */
    public static final String KEY = "test_key";

    private String value;

    public String getValue() {
        return value;
    }

    @Cacheable(cacheNames = "test_cache#600", key = "#root.target.KEY")
    public String getValueByCache() {
        return getValue();
    }

    public void setValue(String value) {
        this.value = value;
    }

    @CacheEvict(cacheNames = TestService.CACHE_NAMES, key = "#root.target.KEY")
    public void setValueAndEvictCache(String value) {
        setValue(value);
    }

    @Autowired
    private RedisTemplate redisTemplate;

}
